<?php
namespace app\admin\model;
use Think\Db;
use think\Model;
use think\captcha\Captcha;

class Users extends Model
{

    /**
     * 用户登录验证
     */
    public function checkLogin()
    {
        $user    = input("post.user");
        $pwd     = input("post.pwd");
        $captcha = new Captcha();
        if (!$captcha->check(input("post.code")))
        {
           return js_return(0, '验证码错误!');
        }
        $rs = $this->where("user", $user)
                ->where(["isLock" => 1])
                ->find();
        if (empty($rs))
            return js_return(0, '账号错误!');
        if ($rs->pwd == md5($pwd . md5($rs->secret_key)))
        { 
            $log = new LogUsersLogin();
            $data              = [];
            $data["userId"]    = $rs->id;
            $data["userUser"]    = $rs->user;
            $data["userName"]    = $rs->name;
            $data["loginTime"] = date('Y-m-d H:i:s');
            $data["loginIp"]   = request()->ip();
            $log->add($data);
            
            $this->update(['last_time'=>date('Y-m-d H:i:s'),'last_ip'=>request()->ip()], 'id=' . $rs->id);
            
            $rules = new Rules();
            $menus = $rules->rules_to_menus($rs->rules);
            cookie("user", $rs->user, 3600 * 24 * 90);
            session('my_self', $rs);
            session('my_menus', $menus);
            //日志
            $name = session('my_self')['name'];
            $sj = date("Y-m-d H:i:s");
            $des = $name."在".$sj."登陆了后台";
            add_log(1,session('my_self')['id'], $des);
            return js_return(1, '登录成功!',['url'=> url("admin/index/home")]);
        }
        return js_return(0, '密码错误!');
    }

    public function getPage($where, $page)
    {
        
    }

    public function getAll($where = null)
    {
        return $this->where($where)->order('id', 'asc')->select()->toArray();
    }

    /**
     * 新增
     */
    public function add()
    {
        if (input('post.user') == '')
        {
            return js_return(0, "账号不能为空");
        }
        if (input('post.name') == '')
        {
            return js_return(0, "昵称不能为空");
        }
        if (input('post.pwd') == '')
        {
            return js_return(0, "密码不能为空");
        }
        $rules = input('post.rules/a');
        $icontypes = input('post.icontypes/a');
//        halt($rules);
        if(empty($rules))
        {
            return js_return(0, "角色不能为空");
        }
        if(empty($icontypes))
        {
            return js_return(0, "编辑类型不能为空");
        }
        $secretKey       = get_secret_key();
        $opt             = new \stdClass;
        $opt->user       = input('post.user');
        $opt->name       = input('post.name');
        $opt->pwd        = md5(input('post.pwd') . md5($secretKey));
        $opt->sort       = input('post.sort');
        $opt->secret_key = $secretKey;
        $opt->rules      = implode(',', input('post.rules/a'));
        $opt->icontypes  = implode(',', input('post.icontypes/a'));
        $result          = $this->save($opt);
        //日志
        $id              = $this->order("id desc")->find();
        $name            = session('my_self')['name'];
        $des             = $name . "【添加】了编号为【" . $id->id . "】的用户";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "添加成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (input('post.user') == '')
        {
            return js_return(0, "账号不能为空");
        }
        if (input('post.name') == '')
        {
            return js_return(0, "昵称不能为空");
        }
        $rules = input('post.rules/a');
//        halt($rules);
        if(empty($rules))
        {
            return js_return(0, "角色不能为空");
        }
        $icontypes = input('post.icontypes/a');
        if(empty($icontypes))
        {
            return js_return(0, "编辑类型不能为空");
        }
        $opt            = new \stdClass;
        $opt->user      = input('post.user');
        $opt->name      = input('post.name');
        if(input('post.pwd') !='')
        {
            $secretKey      = get_secret_key();
            $opt->pwd       = md5(input('post.pwd') . md5($secretKey));
            $opt->secret_key = $secretKey;
        }
        
        $opt->sort      = input('post.sort');
        $opt->rules     = implode(',', input('post.rules/a'));
        $opt->icontypes  = implode(',', input('post.icontypes/a'));
        $result = $this->update($opt, 'id=' . input('id'));
        //日志
        $id = input('post.id');
        $name = session('my_self')['name'];
        $des = $name."【修改】了编号为【".$id."】的用户";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "更新成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    public function del()
    {
        $result = $this->destroy(input('id'));
        if (false !== $result)
        {
            return js_return(1, "删除成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }
    
    //用户信息
    public function getById($id)
    {
        return (Object) $this->where('id', $id)->find()->toArray();
    }

    //用户信息
    public function getByName($name)
    {
        return (Object) $this->where('user', $name)->find()->toArray();
    }
}
